package com.yellow.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yellow.dao.CheckItemDao;
import com.yellow.enity.PageResult;
import com.yellow.enity.QueryPageBean;
import com.yellow.pojo.CheckItem;
import com.yellow.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//注册为服务
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //使用分页助手
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> pageQuery = checkItemDao.pageQuery(queryString);
        long total = pageQuery.getTotal();
        List<CheckItem> rows = pageQuery.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public CheckItem findByid(int id) {
        CheckItem checkItem = checkItemDao.findByid(id);
        return checkItem;
    }

    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }

    @Override
    public void deleteByid(int id) {
        //判断是否检查组的外键关联
        int num = checkItemDao.findCheckGroupByCheckItemId(id);
        if (num > 0) {
            //有关联到检查组
            throw new RuntimeException(id + "--有检查组关联");
        }
        //不存在就删除
        checkItemDao.deleteByid(id);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
